package DP;

/**
 * @author 曹见朋
 * @create 2024-06-12-22:34
 */
public class Main494 {
    // TODO 494. 目标和
    public static int findTargetSumWays(int[] nums, int target) {
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        if ((sum + target) % 2 != 0) return 0;
        int left = (sum + target) / 2;
        int[] dp = new int[left + 1];

        dp[0] = 1;
        for(int i = 0; i < nums.length; i++){
            for(int j = left; j >= nums[i]; j--){
                dp[j] = dp[j] + dp[j - nums[i]];
            }
        }
        return dp[left];
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 1, 1, 1, 1};
        int target = 3;

        int targetSumWays = findTargetSumWays(nums, target);
        System.out.println(targetSumWays);
    }
}
